package com.taoyuan.hotel.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taoyuan.hotel.dao.DepartmentDao;
import com.taoyuan.hotel.dto.DepartmentDto;
import com.taoyuan.hotel.entity.Department;
import com.taoyuan.hotel.excel.DepartmentExcel;
import com.taoyuan.hotel.service.intf.DepartmentService;
import com.taoyuan.hotel.vo.PageBean;
import com.taoyuan.hotel.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao dao;

    @Override
    public R save(Department department) {
        //执行新增操作
        if (dao.add(department) > 0) {
            //成功
            return R.ok();
        } else {
            //失败
            return R.fail();
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Department> pageInfo = new PageInfo<>(dao.all());
        //3.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<Department> queryAll() {
        return dao.all();
    }


    @Override
    public R del(int id) {
        if (id > 0) {
            if (dao.del(id) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        } else {
            return R.fail();
        }
    }

    @Override
    public R update(Department department) {
        if (dao.update(department) > 0) {
            //修改成功
            return R.ok();
        } else {
            //修改失败
            return R.fail();
        }
    }

    @Override
    public PageBean queryPageWhere(String deptName, int page, int limit) {
        //1.验证 查询条件 如果为"" 那么赋值为null
        if (!StringUtils.hasLength(deptName)){
            deptName=null;
        }else {
            deptName="%"+deptName+"%";//包含模糊查询
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<Department> pageInfo=new PageInfo<>(dao.selectWhere(deptName));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if (ids!=null && ids.length>0){
            if (dao.delBatch(ids)>0){
                return R.ok();
            }else {
                return R.fail();
            }
        }else {
            return R.fail();
        }
    }

    public R saveBatch(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            //excel解析
            List<DepartmentExcel> list= EasyExcel.read(file.getInputStream(), DepartmentExcel.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {}
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
            }).sheet().doReadSync();
            //执行dao 批量新增
            if(dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R selectTJ() {
        List<DepartmentDto> list = dao.selectTJ();

        HashMap<String, List<Object>> map = new HashMap<>();
        map.put("deptName", new ArrayList<>());
        map.put("nums", new ArrayList<>());

        list.forEach(f->{
            map.get("deptName").add(f.getDeptName());
            map.get("nums").add(f.getNums());
        });
        return R.ok(map);
    }
    }
